using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spinning.Spinning.Interface;
using Microsoft.Xna.Framework;

namespace Spinning.Spinning.EntityModifiers
{
   public abstract class EntityModifier : IModifier<IEntity>
    {
        int _startTime, _endTime;
        float _count = 0;
        int _updateCount = 0;
        bool _isStart, _isFinish;
        public delegate void ModifierEventHandler(IEntity item, EntityModifier modifier);
        public ModifierEventHandler OnStart, OnFinish;

        //second
        public EntityModifier(int startTime, int endTime)
        {
            _startTime = startTime;
            _endTime = endTime;
            _isStart = _isFinish = false;
            OnStart += onStartFlag;
            OnFinish += onFinishFlag;
        }

        public void OnUpdate(IEntity item, GameTime gameTime)
        {
            _count += (float)gameTime.ElapsedGameTime.Milliseconds / 1000f;
            int count = (int)_count;
            if (count == _startTime && !_isStart && OnStart != null)
                OnStart(item, this);
            if (count == _endTime && !_isFinish && OnFinish != null)
                OnFinish(item, this);

            if(_isStart&&!_isFinish){
                float perSecond = (float)gameTime.ElapsedGameTime.Milliseconds / 1000f;
                float totalSecond = _endTime - _startTime;
                float frames = totalSecond / perSecond;
                uptateEntity(item,frames, _updateCount); 
                _updateCount++;
            }

        }

        void onStartFlag(IEntity item, EntityModifier modifier)
        {
            _isStart = true;
        }

        void onFinishFlag(IEntity item, EntityModifier modifier)
        {
            _isFinish = true;
        }

        protected abstract void uptateEntity(IEntity item,float frames, int count);

    }
}
